package com.lineage.server.command.executor;

import com.lineage.server.command.GMCommands;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Favorite implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Favorite.class);
  
  private static final Map<Integer, String> _faviCom = new HashMap<>();
  
  public static L1CommandExecutor getInstance() {
    return new L1Favorite();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (!_faviCom.containsKey(Integer.valueOf(pc.getId())))
        _faviCom.put(Integer.valueOf(pc.getId()), ""); 
      String faviCom = _faviCom.get(Integer.valueOf(pc.getId()));
      if (arg.startsWith("set")) {
        StringTokenizer st = new StringTokenizer(arg);
        st.nextToken();
        if (!st.hasMoreTokens()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("纪录质不能为空白"));
          return;
        } 
        StringBuilder cmd = new StringBuilder();
        String temp = st.nextToken();
        if (temp.equalsIgnoreCase(cmdName)) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(cmdName)) + " 纪录质异常。"));
          return;
        } 
        cmd.append(String.valueOf(String.valueOf(temp)) + " ");
        while (st.hasMoreTokens())
          cmd.append(String.valueOf(String.valueOf(st.nextToken())) + " "); 
        faviCom = cmd.toString().trim();
        _faviCom.put(Integer.valueOf(pc.getId()), faviCom);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(faviCom)) + " 指令纪录完成!"));
      } else if (arg.startsWith("show")) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前纪录的指令: " + faviCom));
      } else if (faviCom.isEmpty()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前无纪录指令!"));
      } else {
        StringBuilder cmd2 = new StringBuilder();
        StringTokenizer st2 = new StringTokenizer(arg);
        StringTokenizer st3 = new StringTokenizer(faviCom);
        while (st3.hasMoreTokens()) {
          String temp2 = st3.nextToken();
          if (temp2.startsWith("%")) {
            cmd2.append(String.valueOf(String.valueOf(st2.nextToken())) + " ");
            continue;
          } 
          cmd2.append(String.valueOf(String.valueOf(temp2)) + " ");
        } 
        while (st2.hasMoreTokens())
          cmd2.append(String.valueOf(String.valueOf(st2.nextToken())) + " "); 
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(cmd2 + " 指令执行。"));
        GMCommands.getInstance().handleCommands(pc, cmd2.toString());
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
